<?php 
$page_ttle='Edit a URL';
include './includes/header.html';

//check for a url id
if (isset($_GET['uid'])){//page is first accessed
	$uid=(int)$_GET['uid'];
}elseif (isset($_POST['uid'])){
	$uid=(int)$_POST['uid'];
}else{
	$uid=0;
}

if ($uid<=0){
	echo '<p><font color="red">This page has been accessed incorrectly!</font></p> ';
	include './includes/footer.html';
	exit();
}

require_once 'mysql_connect.php';

if (isset($_POST['submitted'])){
	if ($_POST['which']=='delete'){
		$query="delete from urls where url_id=$uid";
		$result=mysql_query($query);
		$affect=mysql_affected_rows();
		
		$query="delete from url_associations where url_id=$uid";
		$result=mysql_query($query);
		$affect+=mysql_affected_rows();
		
		if ($affect>0){
			echo '<p><b>The URL has been deleted</b></p>';
		}else {
			echo '<p><font color="red">Your submission could not be processed</font></p>';
		}
		include './includes/footer.html';
		exit();
	}else {//edit url  default action
		if (eregi('^([[:alnum:]\-\.])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+=%&_\.~?\-]*)$', $_POST['url'])){
			$u=escape_data($_POST['url']);
		}else{
		$u=FALSE;
		echo '<p><font color="red">Please enter a valid URL!</font></p>';
		}
		//check for a url title
		if (!empty($_POST['title'])){
			$t=escape_data($_POST['title']);
		}else{
			$t=FALSE;
			echo '<p><font color="red">Please enter a URL name!</font></p>';
		}
	
		//check for a description
		if (!empty($_POST['description'])){
			$d=escape_data($_POST['description']);
		}else {
			$d=FALSE;
			echo '<p><font color="red">Please enter a description!</font></p>';
		}
	
		//check for a category
		if (isset($_POST['types'])&&(is_array($_POST['types']))){
			$type=TRUE;
		}else {
			$type=FALSE;
			echo '<p><font color="red">Please select at least one category !</font></p>';
		}
	
		if($u&&$t&&$d&&$type){
			$query1="update urls set url='$u',title='$t',description='$d' where url_id=$uid";
			$result1=mysql_query($query1);
			
			$exist_types=unserialize(urldecode($_POST['exist_types']));
			
			if ($_POST['types']!=$exist_types){//a category change was made
				//determine the new and old categories
				$add=array_diff($_POST['types'], $exist_types);
				$delete=array_diff($exist_types, $_POST['types']);
				//add new types,if needed
				if (!empty($add)){
					$query2='insert into url_associations (url_id,url_category_id,approved) values';
					foreach ($add as $v){
						$query2.="($uid,$v,'Y'), ";
					}
					$query2=substr($query2, 0,-2);
					$result2=mysql_query($query2);
				}else{
					$result2=TRUE;
				}
				
				//delete old types
				if (!empty($delete)){
					$query3="delete from url_associations where (url_id=$uid) and (url_category_id in(".implode(',',$delete)."))";
					$result3=mysql_query($query3);
				}else {
					$result3=TRUE;
				}
			}else {//no category changes being made
				$result2=TRUE;
				$result3=TRUE;
			}
			
			//sucess
			if ($result1&&$result2&&$result3){
				echo '<p><b>The URL has been edited!</b></p>';
			}else {
				echo '<p><font color="red">Your submission could not be processed</font></p>';
			}
		}else {//one of data tests failed
			echo '<p><font color="red">Please try again</font></p>';
		}
	}//end edit/delete if else
}

//---display the form---

//retrieve the url's current information
$query="select url,title,description,url_category_id from urls LEFT JOIN url_associations USING (url_id) 
where urls.url_id=$uid";
$result=mysql_query($query);

//get all of the information for the first record
$exist_types=array();
list($url,$title,$desc,$exist_types[])=mysql_fetch_array($result);

//get the other url_category_id values
while ($row=mysql_fetch_array($result)){
	$exist_types[]=$row[3];
}
?>
<form action="edit_url.php" method="post">
	<fieldset><legend>Edit a URL:</legend>
	
	<p><b>Select One:</b><input type="radio" name="which" value="edit" checked="checked"/>Edit <input type
	="radio" name="which" value="delete" />Delete</p>
	
	<p><b>URL:</b><input type="text" name="url" size="60" maxlength="60" value="<?php echo $url; ?>"/></br>
	<small>Do not include the initial<i>http://</i></small></p>
	
	<p><b>URL Name:</b><input type="text" name="title" size="60" maxlength="60" value="<?php echo $title;?>"/></p>
	
	<p><b>Description:</b><textarea name="description" cols="40" rows="5" ><?php echo $desc;?></textarea></p>
	
	<p><b>Category:</b><select name="types[]" multiple="multiple" size="5">
		<?php //create the pull down information 
		$query="SELECT * FROM url_categories ORDER BY category ASC";
		$result=@mysql_query($query);
		while ($row=mysql_fetch_array($result)){
			echo "<option value=\"$row[0]\"";
			if (in_array($row[0], $exist_types)){
				echo 'selected="selected"';
			}
			echo ">$row[1]</option>\n";
		}
		?>
		</select></p>
	</fieldset>
	<input type="hidden" name="submitted" value="TRUE"/>
	<?php 
	echo '<input type="hidden" name="exist_types" value="'.urlencode(serialize($exist_types)).'" />
	<input type="hidden" name="uid" value="'.$uid.'" />
	';
	?>
	<div align="center"><input type="submit" name="submit" value="Submit"/></div>
</form>
<?php 
mysql_close();
include './includes/footer.html';
?>




